﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;
using System.Windows.Forms;

namespace ClippingEditor
{
    /// <summary>
    /// Interaction logic for Browser.xaml
    /// </summary>
    public partial class Browser : System.Windows.Controls.UserControl
    {
        public Browser()
        {
            InitializeComponent();
            this.browser.Navigated += new NavigatedEventHandler(browser_Navigated);
           
        }

        void browser_Navigated(object sender, NavigationEventArgs e)
        {
            HideScriptErrors(this.browser, true);
            this.btn_GoBack.IsEnabled = this.browser.CanGoBack;
            this.btn_GoForward.IsEnabled = this.browser.CanGoForward;
        }

        public void Navigate(string url)
        {
            try
            {
                this.browser.Navigate(url);
            }
            catch (Exception ex)
            { 
            
            }
        }

        private void btn_Stop_Click(object sender, RoutedEventArgs e)
        {
            try { this.browser.InvokeScript("eval", "document.execCommand('Stop');");}
            catch(Exception ex) 
            {
            
            }
        }

        private void btn_Refresh_Click(object sender, RoutedEventArgs e)
        {
            try { this.browser.Refresh(); }
            catch { }
        }

        private void btn_GoForward_Click(object sender, RoutedEventArgs e)
        {
            try { this.browser.GoForward(); }
            catch { }
        }

        private void btn_GoBack_Click(object sender, RoutedEventArgs e)
        {
            try { this.browser.GoBack(); }
            catch { }
        }


        /// <summary>
        /// 隐藏浏览器的脚本错误提示
        /// </summary>
        /// <param name="wb"></param>
        /// <param name="Hide"></param>
        public void HideScriptErrors(System.Windows.Controls.WebBrowser wb, bool Hide)
        {

            FieldInfo fiComWebBrowser = typeof(System.Windows.Controls.WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);

            if (fiComWebBrowser == null) return;

            object objComWebBrowser = fiComWebBrowser.GetValue(wb);

            if (objComWebBrowser == null) return;

            objComWebBrowser.GetType().InvokeMember(

            "Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { Hide });

        }

        public mshtml.HTMLDocument Document
        {
            get { return this.browser.Document as mshtml.HTMLDocument; }
        }
      
    }
}
